package com.smart.community.property.controller;

import com.smart.community.commons.annotation.ApiLog;
import com.smart.community.commons.enums.ApiLogOperationType;
import com.smart.community.commons.result.Result;
import com.smart.community.property.dto.FeeDuplicateCheckResult;
import com.smart.community.property.service.FeeDuplicateCheckService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 费用重复检查控制器
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/property/fees")
@RequiredArgsConstructor
@Tag(name = "费用重复检查", description = "费用重复检查相关接口")
public class FeeDuplicateCheckController {

    private final FeeDuplicateCheckService feeDuplicateCheckService;

    @PostMapping("/check-property-duplicate")
    @Operation(summary = "检查物业费重复")
    @ApiLog(
        logTitle = "检查物业费重复",
        logType = 2,
        moduleName = "物业管理",
        operationType = ApiLogOperationType.QUERY
    )
    public Result<FeeDuplicateCheckResult> checkPropertyFeeDuplicate(
            @RequestParam @NotNull Long communityId,
            @RequestParam @NotNull String billingPeriod,
            @RequestParam @NotNull Integer feeType) throws Exception {
        
        log.info("检查物业费重复，社区ID: {}, 计费周期: {}, 费用类型: {}", communityId, billingPeriod, feeType);
        
        FeeDuplicateCheckResult result = feeDuplicateCheckService.checkPropertyFeeDuplicate(
            communityId, billingPeriod, feeType);
        
        if (result.isHasDuplicate()) {
            return Result.fail("发现重复记录");
        } else {
            return Result.success(result);
        }
    }

    @PostMapping("/check-sanitation-duplicate")
    @Operation(summary = "检查卫生费重复")
    @ApiLog(
        logTitle = "检查卫生费重复",
        logType = 2,
        moduleName = "物业管理",
        operationType = ApiLogOperationType.QUERY
    )
    public Result<FeeDuplicateCheckResult> checkSanitationFeeDuplicate(
            @RequestParam @NotNull Long communityId,
            @RequestParam @NotNull String billingPeriod) throws Exception {
        
        log.info("检查卫生费重复，社区ID: {}, 计费周期: {}", communityId, billingPeriod);
        
        FeeDuplicateCheckResult result = feeDuplicateCheckService.checkSanitationFeeDuplicate(
            communityId, billingPeriod);
        
        if (result.isHasDuplicate()) {
            return Result.fail("发现重复记录");
        } else {
            return Result.success(result);
        }
    }
}
